package cn.ante.ymcc.mq.consumer;

import cn.ante.ymcc.domain.MediaFile;
import cn.ante.ymcc.result.JSONResult;
import cn.ante.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(consumerGroup = "service-media-consumer",topic = "topic-media",
        selectorExpression = "tags-media",messageModel = MessageModel.CLUSTERING) // 默认的消费方式是集群。意思是如果同一个队列有多个消费者，名字都有一样，一样的配置
// 只有一个消费成功，一个消息只会给到一个消费者，即使有十台机器，也只有一个消费者，保证了消费不重复。广播模式是指一个消息可以被多个消费者消费，
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;
    @Override
    public void onMessage(MessageExt messageExt) {
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        if (jsonResult.isSuccess()){
            mediaFile.setFileStatus(2);
        }else {
            mediaFile.setFileStatus(3);
        }
        mediaFileService.updateById(mediaFile);
        }
}
